package com.example.tourstor;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CheckActivity extends AppCompatActivity {
    private ListView check_list;
    private List<Group> checkList = new ArrayList<Group>();
    View view;
    int UID;
    Button display;
    Thread t;
    String mqrs;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check);

        //获取上一页面传来的UID
        Intent intent=getIntent();
        //UID=intent.getStringExtra("U_id");
        UID=2;

        display = findViewById(R.id.display_more);
        check_list = (ListView) findViewById(R.id.check_list);   //获得子布局

        checkList.clear();

        getData();

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what==1){
                    GroupAdapter groupAdapter1 = new GroupAdapter(CheckActivity.this,
                            R.layout.group_item, checkList);  //关联数据和子布局
                    check_list.setAdapter(groupAdapter1);   //绑定数据和适配器
                }
            }
        };



        check_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {  //点击每一行的点击事件
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position,
                                    long id) {
                final Group group1=checkList.get(position);         //获取点击的那一行
                final int GID = group1.getGid();
                final EditText et_Chufa = new EditText(CheckActivity.this);
                et_Chufa.setText(group1.getChufa());
                final EditText et_Mudidi = new EditText(CheckActivity.this);
                et_Mudidi.setText(group1.getMudidi());
                final EditText et_Feiyong = new EditText(CheckActivity.this);
                et_Feiyong.setText(group1.getFeiyong());
                final EditText et_Dianhua = new EditText(CheckActivity.this);
                et_Dianhua.setText(group1.getDianhua());
                final EditText et_Renshu = new EditText(CheckActivity.this);
                et_Renshu.setText(group1.getRenshu());
                AlertDialog.Builder builder=new AlertDialog.Builder(CheckActivity.this);
                builder.setTitle("修改"+group1.getGid()+"号团队信息");
                /*builder.setMessage("出发时间：");
                builder.setView(et_Chufa);
                builder.setMessage("目的地：");
                builder.setView(et_Mudidi);
                builder.setMessage("预计人均费用：");
                builder.setView(et_Feiyong);
                builder.setMessage("联系方式：");
                builder.setView(et_Dianhua);
                builder.setMessage("人数上限：");
                builder.setView(et_Renshu);
                final EditText editText1=new EditText(CheckActivity.this);
                editText1.setHint();
                final EditText editText2=new EditText(CheckActivity.this);
                editText2.setHint((CharSequence) et_Mudidi);
                final EditText editText3=new EditText(CheckActivity.this);
                editText3.setHint((CharSequence) et_Feiyong);
                final EditText editText4=new EditText(CheckActivity.this);
                editText4.setHint((CharSequence) et_Dianhua);
                final EditText editText5=new EditText(CheckActivity.this);
                editText5.setHint((CharSequence) et_Renshu);*/
                LinearLayout layout=new LinearLayout(CheckActivity.this);
                layout.setOrientation(LinearLayout.VERTICAL);
                layout.addView(et_Chufa);
                layout.addView(et_Mudidi);
                layout.addView(et_Feiyong);
                layout.addView(et_Dianhua);
                layout.addView(et_Renshu);
                builder.setView(layout);

                builder.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                final String chufa = et_Chufa.getText().toString();
                                final String mudidi = et_Mudidi.getText().toString();
                                final String feiyong = et_Feiyong.getText().toString();
                                final String dianhua = et_Dianhua.getText().toString();
                                final String renshu = et_Renshu.getText().toString();
                                int result = renshu.compareTo(mqrs);
                                if (chufa.equals("")||mudidi.equals("")||feiyong.equals("")||dianhua.equals("")||renshu.equals("")){
                                    Toast.makeText(getApplicationContext(), "修改内容不能为空！" , Toast.LENGTH_LONG).show();
                                }else if(result<0){
                                    Toast.makeText(getApplicationContext(), "目前人数已超，不得修改" , Toast.LENGTH_LONG).show();
                                } else{
                                    new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            try {
                                                java.sql.Connection cn = DBOpenHelper.getConn();
                                                String sql3 = "update team set setOutTime=?,destination=?,cost=?,tell=?,number=? where G_id=?";
                                                PreparedStatement st3 = cn.prepareStatement(sql3);
                                                st3.setString(1, chufa);
                                                st3.setString(2, mudidi);
                                                st3.setString(3, feiyong);
                                                st3.setString(4, dianhua);
                                                st3.setString(5, renshu);
                                                st3.setInt(6, GID);
                                                st3.executeUpdate();
                                            } catch (SQLException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }).start();
                                    showDialog2();
                                }
                            }
                        });
                builder.setNegativeButton("取消", null);
                AlertDialog dialog=builder.create();
                dialog.show();
            }
        });

        display.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.putExtra("U_id", UID);
                intent.setClass(CheckActivity.this, DisplayActivity.class);
                CheckActivity.this.startActivity(intent);
            }
        });

    }

    private void getData(){
        t=new Thread(new Runnable() {
           @Override
            public void run() {
                try {
                    java.sql.Connection cn = DBOpenHelper.getConn();
                    String sql = "select * from team where U_id= '"+UID+"'";
                    PreparedStatement st = cn.prepareStatement(sql);
                    //st.setString(1,UID);
                    ResultSet rs = st.executeQuery();
                    if (rs != null) {
                        while (rs.next()) {
                            int gid = rs.getInt("G_id");
                            String time = rs.getString("setOutTime");
                            String des = rs.getString("destination");
                            String c = rs.getString("cost");
                            String t = rs.getString("tell");
                            String r = rs.getString("number");
                            String sql2 = "select * from Gruop_User where G_id=?";
                            PreparedStatement st2 = cn.prepareStatement(sql2);
                            st2.setInt(1,gid);
                            ResultSet rs2 = st2.executeQuery();
                            if(rs2==null){
                                checkList.add(new Group(gid,des,time,c,t,r,"1"));
                            }else{
                                int mq=1;
                                while(rs2.next()){
                                    mq++;
                                }
                                mqrs = String.valueOf(mq);
                                checkList.add(new Group(gid,des,time,c,t,r,mqrs));
                            }
                        }
                    }
                    //com.wujiajia.lvyou.DBOpenHelper.closeAll(cn, st);
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.print("失败");
                }
               Message msg = handler.obtainMessage(1);
                msg.what = 1;
                //msg.obj = bundle;
                handler.sendMessage(msg);
            }
       });
        t.start();

    }

    private void showDialog2(){
        AlertDialog.Builder builder =new AlertDialog.Builder(this);
        builder.setTitle("修改成功");
        builder.setMessage("您已成功修改该团队信息");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent intent = new Intent(CheckActivity.this,MainActivity.class);
                intent.putExtra("U_id",UID);
                startActivity(intent);
            }
        });
        AlertDialog dialog=builder.create();
        dialog.show();
    }

}
